/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ApuestaVarios.java
 *
 * Created on 08-ene-2012, 14:01:02
 */

package GUI;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import logica.Apuesta;
import logica.Evento;
import logica.Participante;

/**
 *
 * @author andresgodoysalavert
 */
public class ApuestaVarios extends javax.swing.JFrame {

    Evento evento = new Evento();
    Participante participante = new Participante();
    Apuesta apuesta = new Apuesta();

    public void llenarTabla(int idEv) throws SQLException{
        modeloTabla = evento.getParticipantes(idEv);
    }



    /** Creates new form ApuestaVarios */
    public ApuestaVarios(int id) throws SQLException {
        modeloTabla = new DefaultTableModel();
        initComponents();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds(0,0,screenSize.width, (int) (screenSize.height));
        this.jLabel1.setBounds(0,0,screenSize.width, screenSize.height-100);
        this.jLayeredPane1.setBounds(0,0,screenSize.width, screenSize.height);
        this.jPanel1.setBounds(0,0,screenSize.width, screenSize.height);
        this.jPanel1.setLayout(null);
        this.jScrollPane1.setBounds(120, 100, screenSize.width/2, screenSize.height/2+100);
        this.jButton1.setBounds(125+this.jScrollPane1.getWidth(), 100, 70, 70);
        this.jButton2.setBounds(125+this.jScrollPane1.getWidth(), 170, 70, 70);
        this.jToggleButton1.setBounds(125+this.jScrollPane1.getWidth(), 240, 70, 70);
        this.jButton3.setBounds((screenSize.width*1/2)-250,screenSize.height-230,150,50);
        this.jButton4.setBounds((screenSize.width*1/2)+100,screenSize.height-230,150,50);
        this.jButton3.setText("Volver");
        this.jButton4.setText("Confirmar");
        this.jTable1.getTableHeader().setPreferredSize(new Dimension(this.jScrollPane1.getWidth(), 30));
        this.jTable1.getTableHeader().setPreferredSize(new Dimension(this.jScrollPane1.getWidth(), 30));
        this.jTable1.setFont(new java.awt.Font("Lucida Grande", 0, 15));
        this.jTable1.getTableHeader().setFont(new java.awt.Font("Lucida Grande", 1, 15));
        this.jLabel2.setBounds(960,100,400,100);
        this.jLabel3.setText(String.valueOf(id));
        this.jScrollPane2.setBounds(960,300,300,200);
        this.jLabel4.setBounds(960, 220, 300, 50);
        this.jLabel5.setBounds(960, 220+this.jLabel4.getHeight(), 300, 20);
        this.jTextArea1.setLineWrap(true);
        this.jTextArea1.setWrapStyleWord(true);
        this.jLabel6.setText(String.valueOf(id));
        setResizable(false);
        llenarTabla(id);
        this.jTable1.setModel(modeloTabla);
        this.jTable1.removeColumn(this.jTable1.getColumn("Id"));
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jToggleButton1 = new javax.swing.JToggleButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(153, 153, 153));
        jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jTable1.setBackground(new java.awt.Color(204, 204, 204));
        jTable1.setModel(modeloTabla);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/uparrow.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/downarrow.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/backarrow.png"))); // NOI18N
        jButton3.setText("jButton3");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/check.png"))); // NOI18N
        jButton4.setText("jButton4");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/farana small 3.png"))); // NOI18N

        jLabel3.setForeground(new java.awt.Color(153, 153, 153));
        jLabel3.setText("jLabel3");

        jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/vsblacksmall.png"))); // NOI18N
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Lucida Grande", 2, 18));

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 1, 18));
        jLabel4.setText("Descripcion:");

        jScrollPane2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jTextArea1.setBackground(new java.awt.Color(204, 204, 204));
        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Lucida Grande", 2, 18));
        jTextArea1.setRows(5);
        jScrollPane2.setViewportView(jTextArea1);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/marco3.png"))); // NOI18N

        jLabel6.setForeground(new java.awt.Color(153, 153, 153));
        jLabel6.setText("jLabel6");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jButton2)
                        .add(89, 89, 89))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                .add(jPanel1Layout.createSequentialGroup()
                                    .add(jButton3)
                                    .add(35, 35, 35)
                                    .add(jToggleButton1))
                                .add(jPanel1Layout.createSequentialGroup()
                                    .add(jButton4)
                                    .add(224, 224, 224)
                                    .add(jLabel2)
                                    .add(849, 849, 849)))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(187, 187, 187)
                                .add(jLabel6)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel3)))
                        .add(849, 849, 849)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 442, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)))
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 255, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(792, 792, 792)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jLabel4)
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(24, 24, 24)
                            .add(jLabel5)))
                    .add(127, 127, 127)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(0, 1521, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(0, 0, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 327, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(35, 35, 35)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jButton3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jButton4)
                                    .add(jLabel2)))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(19, 19, 19)
                                .add(jToggleButton1)
                                .add(53, 53, 53)
                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                    .add(jLabel3)
                                    .add(jLabel6)))
                            .add(jPanel1Layout.createSequentialGroup()
                                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 460, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(jButton2)))))
                .addContainerGap(232, Short.MAX_VALUE))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(343, 343, 343)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(37, 37, 37)
                            .add(jLabel4)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jLabel5)))
                    .addContainerGap(343, Short.MAX_VALUE)))
            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(jPanel1Layout.createSequentialGroup()
                    .add(0, 0, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(0, 0, Short.MAX_VALUE)))
        );

        jPanel1.setBounds(0, 0, 680, 350);
        jLayeredPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void acomodarPosiciones(){
        int pos=0;
        while(pos<jTable1.getRowCount()){
            jTable1.setValueAt(pos+1, pos, 0);
            pos++;
        }
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String aux = "";
        if(jLabel5.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"Debe seleccionar un participante para moverlo","Error",JOptionPane.WARNING_MESSAGE);
        }else{
            if (jTable1.getSelectedRow()==0){
                modeloTabla.moveRow(jTable1.getSelectedRow(), jTable1.getSelectedRow(), jTable1.getRowCount()-1);
                jTable1.changeSelection(jTable1.getRowCount()-1, 0, false, false);
            }else{
                modeloTabla.moveRow(jTable1.getSelectedRow(), jTable1.getSelectedRow(), jTable1.getSelectedRow()-1);
                jTable1.changeSelection(jTable1.getSelectedRow()-1, 0, false, false);
            }
        acomodarPosiciones();
        }
        


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        // TODO add your handling code here:
        if (this.jToggleButton1.isSelected()){
            jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/vsgoldsmall.png")));
            JOptionPane.showMessageDialog(null,"Ha sido ACTIVADO boton 'A GANADOR', solo apostara por el primer lugar!","Ganador",JOptionPane.WARNING_MESSAGE);
        }else{
            jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/images/vsblacksmall.png")));
            JOptionPane.showMessageDialog(null,"Ha sido DESACTIVADO boton 'A GANADOR', apostara por tabla de resultados!","Tabla de Resultados",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            participante.getDescripcionParticipante(Integer.parseInt(modeloTabla.getValueAt(jTable1.getSelectedRow(), 0).toString()));
            this.jTextArea1.setText(participante.getDescripcion());
            this.jLabel5.setText(jTable1.getValueAt(jTable1.getSelectedRow(), 1).toString());
        } catch (SQLException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        
        String aux = "";
        if(jLabel5.getText().isEmpty()){
            JOptionPane.showMessageDialog(null,"Debe seleccionar un participante para moverlo","Error",JOptionPane.WARNING_MESSAGE);
        }else{
            if (jTable1.getSelectedRow()==(jTable1.getRowCount()-1)){
                modeloTabla.moveRow(jTable1.getSelectedRow(), jTable1.getSelectedRow(), 0);
                jTable1.changeSelection(0, 0, false,false);
            }else{
                modeloTabla.moveRow(jTable1.getSelectedRow(), jTable1.getSelectedRow(), jTable1.getSelectedRow()+1);
                jTable1.changeSelection(jTable1.getSelectedRow()+1, 0, false,false);
            }
        acomodarPosiciones();

        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        try {
            // TODO add your handling code here:
            this.setVisible(false);
            GUI.Principal p = new GUI.Principal(null);
            p.setVisible(true);
        } catch (SQLException ex) {
            Logger.getLogger(ApuestaVS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        try{
            String factura = "        Farana Bets C.A.\n    Caracas - Venezuela RIF - J-00012255-5\n       La Urbina";
            int monto=Integer.parseInt(JOptionPane.showInputDialog(null,"Inserte el monto a apostar: ","Monto de Apuesta",JOptionPane.WARNING_MESSAGE));
            if (jToggleButton1.isSelected()){
                int max=evento.getMontoApuesta(Integer.parseInt(jLabel6.getText().toString()),Integer.parseInt(modeloTabla.getValueAt(0, 0).toString()));
                    if((max<monto) && (monto>0)){
                        JOptionPane.showMessageDialog(null,"No puede apostar esa cantidad!","Error",JOptionPane.WARNING_MESSAGE);
                    }else{
                        apuesta.insertarApuesta(Integer.parseInt(jLabel6.getText()),Integer.parseInt(modeloTabla.getValueAt(0, 0).toString()), monto, 1);  
                        factura = factura + apuesta.getApuesta() + "\n"+ evento.getParticipanteFactura(Integer.parseInt(jLabel6.getText()),Integer.parseInt(modeloTabla.getValueAt(0, 0).toString()));
                        pdf p=new pdf(factura);
                        p.setVisible(true);
                        p.setBounds(500, 250, 400, 400);
                        p.setDefaultCloseOperation(EXIT_ON_CLOSE);
                        p.setLocationRelativeTo(null);
                        this.setVisible(false);
                        Principal principal = new Principal(null);
                        p.setVisible(true);
                    }
            }else{
                int max=evento.getMontoApuesta(Integer.parseInt(jLabel6.getText().toString()),Integer.parseInt(modeloTabla.getValueAt(0, 0).toString()));
                    if((max<monto) && (monto>0)){
                        JOptionPane.showMessageDialog(null,"No puede apostar esa cantidad!","Error",JOptionPane.WARNING_MESSAGE);
                    }else{
                        
                        int i = modeloTabla.getRowCount();
                        int posicion = 0;
                        while (i>posicion){
                            factura = factura + "\n" + evento.getParticipanteFactura(Integer.parseInt(jLabel6.getText()),Integer.parseInt(modeloTabla.getValueAt(posicion, 0).toString()));
                            apuesta.insertarApuesta(Integer.parseInt(jLabel6.getText()),Integer.parseInt(modeloTabla.getValueAt(posicion, 0).toString()), monto, posicion+1);
                            posicion++;
                        }
                        factura = apuesta.getApuesta()+ factura;
                        pdf p=new pdf(factura);
                        p.setVisible(true);
                        p.setBounds(500, 250, 400, 400);
                        p.setDefaultCloseOperation(EXIT_ON_CLOSE);
                        p.setLocationRelativeTo(null);
                        this.setVisible(false);
                        Principal principal = new Principal(null);
                        p.setVisible(true);
                    }
            }

        } catch (IOException ex) {
            Logger.getLogger(ApuestaVarios.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ApuestaVarios.class.getName()).log(Level.SEVERE, null, ex);
        }catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null,"Solo puede introducir numeros!","Error",JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new ApuestaVarios(0).setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(ApuestaVarios.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables
    public DefaultTableModel modeloTabla;
}
